package com.example.spring_xx_u_market;

import com.example.bean.ErrorCodeV;
import com.example.bean.GoodsDetailV;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import util.UidCtrl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.example.spring_xx_u_market.SpringXxUMarketApplication.jdbcTemplate;

@Slf4j
@RestController
@RequestMapping("api")
public class GoodsDetailVCtrl {
    @PostMapping("goodsDetail")
    public Object getGoodsDetail(HttpServletRequest request) {
        int goods_id;
        String uid = request.getParameter("uid");
        String goods_idS = request.getParameter("goods_id");

        if (goods_idS == null || uid == null) {
            return new ErrorCodeV("005");
        } else {
            goods_id = Integer.parseInt(goods_idS);
            switch (UidCtrl.checkUid(uid)) {
                case "-2":
                    return new ErrorCodeV("003");
                case "-1":
                    return new ErrorCodeV("002");
                case "0":
                    return new ErrorCodeV("004");
                default: {
                    List<GoodsDetailV> query = jdbcTemplate.query("select goods_id,type,name,image_urls,low,high,during_time,currency_type,detail_info,goods_type,phone,start_time,end_time,free_time,busy_time,rec_address,state " +
                                    "from second_hand.goods_info,second_hand.time_info,second_hand.price_info,second_hand.user_info " +
                                    "where goods_info.time_info_id=time_info.time_id and price_info_id=price_info.price_id and user_info.account=goods_info.account and goods_id=?",
                            new BeanPropertyRowMapper<>(GoodsDetailV.class), goods_id);
                    if (query.size() == 0) {
                        return new ErrorCodeV("006");
                    } else {
                        GoodsDetailV goodsDetailV = query.get(0);
                        goodsDetailV.setError_code(0);
                        return goodsDetailV;
                    }
                }
            }
        }
    }
}
